package com.hankol.material.fragment

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseLazyFragment
import com.baselibrary.manager.LoadingManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.SpringViewNewFooter
import com.baselibrary.utils.SpringViewNewHeader
import com.hankol.material.R
import com.hankol.material.adapter.TabMaterialAdapter
import com.hankol.material.api.MaterialConstants
import com.hankol.material.bean.LevelOneBean
import com.hankol.material.databinding.FragmentTabMaterialBinding
import com.hankol.material.viewmodel.TabMaterialViewModel
import com.liaoinstan.springview.widget.SpringView
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**Tab--素材库
 * @author: merbng
 * @time: 2021/10/15 10:36
 * @description:
 */
class TabMaterialFragment : BaseLazyFragment(true) {

    lateinit var binding: FragmentTabMaterialBinding
    private var viewModel: TabMaterialViewModel? = null
    val adapter: TabMaterialAdapter by lazy { TabMaterialAdapter() }
    private var hasnext: Boolean = false

    companion object {
        fun newInstance(): TabMaterialFragment {
            return TabMaterialFragment()
        }
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

        binding = getDataBinding()
        viewModel = getFragmentViewModelProvider(this).get(TabMaterialViewModel::class.java)
        binding.rvOrder.layoutManager = LinearLayoutManager(activity)
        binding.rvOrder.adapter = adapter
        adapter.setOnItemClickListener { adapter, view, position ->
            val mBundle = Bundle()
            mBundle.putParcelable(MaterialConstants.FOLDER_SCREEN, adapter.data[position] as LevelOneBean)
            ActivityRouter.startActivity(ActivityRouter.MATERIAL.MATERIAL_CONTENT_LIST, mBundle)
        }
        binding.smartRefresh.setOnRefreshListener {
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }
        viewModel?.dataListLD?.observe(this, Observer {
            if (it != null) {
                adapter.setNewData(it)
            }
            binding.smartRefresh.finishRefresh()
            binding.smartRefresh.finishLoadMore()
            LoadingManager.hideLoadingDialog(activity)
        })
    }

    override fun lazyLoadData() {
        LoadingManager.showLoadingDialog(activity)
        loadData()
    }

    fun loadData() {
        viewModel?.reqMaterialList()
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_tab_material
    }


    override fun onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        super.onDestroy()
    }

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(messageBus: MessageBus) {

    }
}

